package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.CDBInfo;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Functions;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Jbqk01temp;
import com.hbyj.hbm.vo.Wdwzforryd;
import com.hbyj.hbm.vo.Wdwzforzhdn;
import com.hbyj.hbm.vo.YjczYjzy01;
import com.hbyj.hbm.vo.YjczYjzy02;
import com.hbyj.hbm.vo.YjczYpxx01;
import com.hbyj.hbm.vo.YjdcGpsline;
import com.hbyj.hbm.vo.YjdcGpslineforzhd;
import com.hbyj.hbm.vo.YjdcYjdc01;
import com.hbyj.object.ZTreeNode;
import com.hbyj.util.ChangeTool;
import com.hbyj.util.Constants;
import com.hbyj.util.JsonUtil;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class YjdcBaseInfoAction extends ActionSupport{
	private int total;
	private List rows = new ArrayList();
	private String jsonResult;
	private Jbqk01 jbqk = new Jbqk01();
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public String findYjdc01ListByCondition(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String areaCode = request.getParameter("region");
		areaCode = ChangeTool.ChangeArea(areaCode);
		String sTime = request.getParameter("sTime");
		String eTime = request.getParameter("eTime");//
		String conStr = "";
		if(!"".equals(sTime)){
			conStr += " and yjdc01270 >='"+sTime+"' ";
		}
		if(!"".equals(eTime)){
			conStr += " and yjdc01270 <='"+eTime+"'";
		}
		String hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170,jbqk01.jbqk0180,jbqk01.jbqk0190,jbqk01.id.jbqk0102 from Jbqk01 as jbqk01,YjdcYjdc01 as yjdc where jbqk01.id.jbqk0102=yjdc.jbqk0102 and jbqk01.id.jbqk0100=yjdc.yjdc01000 and yjdc.yjdc01000 like '"+areaCode+"%'"+conStr;
		String countHql = "select count(distinct jbqk01.jbqk0110)   from Jbqk01 as jbqk01,YjdcYjdc01 as yjdc  where jbqk01.id.jbqk0102=yjdc.jbqk0102 and jbqk01.id.jbqk0100=yjdc.yjdc01000 and yjdc.yjdc01000 like '"+areaCode+"%'"+conStr;

		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		//List<List> list = (List)DbTools.findPageHQL(first, max,hql);
		rows = DbTools.parseBaleInfoObj(DbTools.findPageHQL(first, max,hql));
		total = t;
		return SUCCESS;
	}
	public String findYjdcDetailsByCode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String zhdName = request.getParameter("zhdName");
		String hql = " from Jbqk01 jbqk ,YjdcYjdc01 yjdc  where jbqk0100=yjdc01000 and jbqk.id.jbqk0102=yjdc.jbqk0102 and yjdc01000='"+code+"'";
		rows = DbTools.findPageHQL(0,1000,hql);
		
		request.setAttribute("yjdcDetails", rows);
		request.setAttribute("zhdName", zhdName);
		return SUCCESS;
	}
	public void queryYjdc3jbxxByTybh() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String tybh = request.getParameter("tybh");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String hql = "from YjdcYjdc01 where yjdc01000 = '"+tybh+"'";
		if(!startTime.equals("")){
			hql = hql +" and yjdc01270>'"+startTime+"'";
		}
		if(!endTime.equals("")){
			hql = hql +" and yjdc01270<'"+endTime+"'";
		}
		hql = hql +" order by yjdc01270";
		List<YjdcYjdc01> list = DbTools.findByHql(hql);
		JSONArray jsonArray = new JSONArray();
		if(list.size()>0){
			for(int i = 0;i<list.size();i++){
				jsonArray.add(list.get(i));
			}
		}
		ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
	}
	
	
	
	public void queryLishiguiji() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String hql = "from YjdcGpsline";
		if(!("".equals(startTime)&&"".equals(endTime))){
			hql = "from YjdcGpsline where ";
			if(!"".equals(startTime)){
				hql = hql +"time >= '"+startTime+"' AND ";
			}
			if(!"".equals(endTime)){
				hql = hql +"time <= '"+endTime+"' AND ";
			}
			hql = hql.substring(0, hql.length()-4);
		}
		List<YjdcGpsline> list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray jsonArray = new JSONArray();
			for(int i = 0;i<list.size();i++){
				YjdcGpsline gpsline = list.get(i);
				jsonArray.add(gpsline);
			}
			ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
		}
	}
	
	
	public void queryRyd() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String hql = "from Wdwzforryd";
		if(!("".equals(startTime)&&"".equals(endTime))){
			hql = "from Wdwzforryd where ";
			if(!"".equals(startTime)){
				hql = hql +"dwsj >= '"+startTime+"' AND ";
			}
			if(!"".equals(endTime)){
				hql = hql +"dwsj <= '"+endTime+"' AND ";
			}
			hql = hql.substring(0, hql.length()-4);
		}
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				Wdwzforryd ryd = (Wdwzforryd) list.get(i);
				array.add(ryd);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	
	public void queryWdwzforzhdnByCondition() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String eTime = request.getParameter("eTime");
		String sTime =request.getParameter("sTime");
		String zhdbm = request.getParameter("zhdbm");
		String zhdBiaoshi = request.getParameter("zhdBiaoshi");
		String hql = "from Wdwzforzhdn where zhdbm = '"+zhdbm+"' and jbqk0102 ='"+zhdBiaoshi+"'";
		if(!"".equals(sTime)){
			hql = hql +" and dwsj >= '"+sTime+"'";
		}
		if(!"".equals(eTime)){
			hql = hql +" and dwsj <= '"+eTime+"'";
		}
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				Wdwzforzhdn zhd = (Wdwzforzhdn) list.get(i);
				array.add(zhd);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	public void queryGpslineforzhdByCondition() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String eTime = request.getParameter("eTime");
		String sTime =request.getParameter("sTime");
		String zhdbm = request.getParameter("zhdbm");
		String zhdBiaoshi = request.getParameter("zhdBiaoshi");
		String hql = "from YjdcGpslineforzhd where zhdbh = '"+zhdbm+"' and jbqk0102 = '"+zhdBiaoshi+"'";
		if(!"".equals(sTime)){
			hql = hql +" and dwsj >= '"+sTime+"'";
		}
		if(!"".equals(eTime)){
			hql = hql +" and dwsj <= '"+eTime+"'";
		}
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				YjdcGpslineforzhd zhd = (YjdcGpslineforzhd) list.get(i);
				array.add(zhd);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	
	
}
